package com.jason.weibosender;

import weibo4j.Timeline;
import weibo4j.model.Paging;
import weibo4j.model.Status;
import weibo4j.model.StatusWapper;
import weibo4j.model.WeiboException;
import weibo4j.util.WeiboConfig;

public class CatchService implements Runnable{
	final static int TOTAL = 15;
	boolean hadSend = false;
	@Override
	public void run() {
		String accessToken = WeiboConfig
		.getValue("accessToken");
		Timeline tm = new Timeline();
		tm.client.setToken(accessToken);
		int times = 0;
		while(true){
			times ++;
			System.out.println("检测中..." + times);
			try {
				checkTimeLines(tm, "小说漫画", "8");
				checkTimeLines(tm, "精品小说", "7");
				checkTimeLines(tm, "全球小说", "6");
				checkTimeLines(tm, "小说推选", "5");
				checkTimeLines(tm, "口袋小说", "4");
				checkTimeLines(tm, "微小说", "3");
			} catch (WeiboException e) {
				e.printStackTrace();
			}
			try {
				Thread.sleep(3 * 60 * 1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	private void checkTimeLines(Timeline tm, String name, String id) throws WeiboException{
		String sinceID = WeiboConfig
		.getValue(id + "sinceID");
		long since = (sinceID == null || sinceID.length() <= 0) ? 1 : Long.valueOf(sinceID);
		StatusWapper status = tm.getUserTimelineByName(name, new Paging(1, TOTAL), 0, 0);
		long time = 1;
		int size = status.getStatuses().size();
		long largeTime = since;
		if(size > TOTAL - 1){
			time = status.getStatuses().get(TOTAL - 1).getCreatedAt().getTime();
			largeTime = time;
			WeiboConfig.updateProperties(id + "sinceID", String.valueOf(time));
			status.getStatuses().remove(0);
		}
		size = status.getStatuses().size();
		Status s;
		for(int i = size - 1; i >= 0; i--){
			s = status.getStatuses().get(i);
			String text = s.getText();
			long timeC = s.getCreatedAt().getTime();
			String pic = s.getOriginalPic();
			if(timeC > largeTime){
				largeTime = timeC;
				WeiboConfig.updateProperties(id + "sinceID", String.valueOf(largeTime));
			}
			if(! text.contains("://") && 
					! text.contains("网址:") &&
					! text.contains(".com")  &&
					! text.contains("点击")  &&
					! text.contains("淘宝")  && 
					! text.contains("地址")  && 
					! text.contains("转发")  &&
					timeC > since){
				try{
					if(pic != null && pic.length() > 0 && (text.contains("图") || text.contains("转")|| 
							text.length() < 20)){
						if(hadSend){
							try {
								Thread.sleep(3 * 60 * 1000);
							} catch (InterruptedException e) {
								e.printStackTrace();
							}
						}
						hadSend = true;
						tm.Repost(s.getId());
						System.out.println("【" + timeC + "】---转图成功:" + text);
//						System.out.println("带图放弃:" + text);
						
						/*try {
							byte[] data = (new DownloadAgent()).downloadFile(pic, "GET", null, null, null, null);
							if(data == null){
								tm.UpdateStatus(text);
								System.out.println("成功转发:" + text);
								
							} else {
								tm.UploadStatus(text, new ImageItem(name, data));
								System.out.println("成功带图转发:" + text);
							}
						} catch (IOException e) {
							tm.UpdateStatus(text);
							System.out.println("成功转发:" + text);
						}*/
					} else if(text.length() > 35){
						if(hadSend){
							try {
								Thread.sleep(8 * 60 * 1000);
							} catch (InterruptedException e) {
								e.printStackTrace();
							}
						}
						hadSend = true;
						tm.UpdateStatus(text);
						System.out.println("【" + timeC + "】---成功:" + text);
					}
				} catch (WeiboException e) {
					e.printStackTrace();
				}
			} else {
//				System.out.println("【" + timeC + "】---放弃:" + text);
			}
		}
	}
}
